/*
 * @Author: Haochen
 * @Date: 2021-11-21 22:16:32
 * @LastEditTime: 2021-11-26 16:19:53
 * @FilePath: \front\src\js\exam_info.js
 */

import $ from './jquery-3.6.0.min.js';
import '../scss/common.scss';
import '../scss/exam-info.scss';
import { getPromise, backToIndex, getPromiseAuthBy } from './common.js';

backToIndex();

const testType = window.location.search
  .substring(1)
  .split('?')[0]
  .split('=')
  .pop();

var query = window.location.search.substring(1).split('=').pop();

$(function () {
  var strDataQuery = '/testsRouter/:' + query;

  async function getDataAndRender() {
    const { code, data, message } = await getPromise(strDataQuery, 'get', {});

    render(data);
  }

  getDataAndRender();

  async function render(data) {
    const users = JSON.parse(localStorage.getItem('users'));

    const userId = users.find((user) => user.status === 'online')._id;

    const token = localStorage.token;

    const { code, user, message } = await getPromiseAuthBy(
      '/students/:' + userId,
      token,
      'get',
      {}
    );

    $('.nav-main').append(`
        <h3>${data[0].title}</h3>
    `);

    $('.container-1').append(`
   <div class="content-box">
                    <div class="cb-row">
                        试卷信息
                    </div>
                    <div class="cb-row">
                        考试类型：<span>${data[0].typeId.type}</span>
                    </div>
                    <div class="cb-row">
                        考试时间：<span>${data[0].date}</span>
                    </div>
                    <div class="cb-row">
                        答卷时间：<span>${data[0].time}</span>
                    </div>
                    <div class="cb-row">
                        考试方式：<span>线上</span>
                    </div>
                </div>

                <div class="content-box">
                    <div class="cb-row">
                        考生信息
                    </div>
                    <div class="cb-row">
                        姓名：<span>${user[0].name}</span>
                    </div>
                    <div class="cb-row">
                        证件号码：<span>532101199103014545</span>
                    </div>
                    <div class="cb-row">
                        部门：<span>${user[0].dept}</span>
                    </div>
                    <div class="cb-row">

                    </div>
                </div>
  
    `);
  }
});

$('#start-exam').click(function () {
  location.href = `./exam_paper.html?type=${testType}?id=${query}`;
});

$('.btn-back-to-exam').click(function () {
  location.href = `./exam.html?type=${testType}`;
});
